package com.gin.kotlin.no_gay_alliance.fragment.topiclist

import android.os.Bundle
import android.os.Message
import android.util.Log
import androidx.annotation.IntRange
import androidx.viewbinding.ViewBinding
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.TopicListActivity
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.enums.HandlerMessageWhat
import com.gin.nga.response.topic.CurrentForum

/**
 * * 主题列表公共父类
 * * 处理与活动的交互: 翻页跳转，刷新操作，更新当前页、最大页
 * @param T : ViewBinding
 * @property currentPageFragment  当前的页Fragment
 * @property argument 参数
 * @property currentForum 当前版面信息
 * @property viewPager2 ViewPager2
 * @property totalPage 总页数
 * @property currentPage 当前页
 */

abstract class AbstractTopicListFragment<T : ViewBinding> : AbstractBindingFragment<T>() {
    lateinit var currentPageFragment: TopicPageFragment
    protected lateinit var argument: TopicListArgument
    protected var viewPager2: ViewPager2? = null
    protected var currentForum: CurrentForum? = null
    protected var totalPage = 1
    private var currentPage: Int = 1

    override fun onCreateView() {
        this.viewPager2 = b?.root?.findViewById(R.id.view_pager2)
        configViewPager2Adapter()
    }

    /**
     * 为ViewPager2装载适配器
     */
    fun configViewPager2Adapter() {
        this.viewPager2?.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = totalPage
            override fun createFragment(position: Int) = TopicPageFragment.newInstance(argument.copy(page = position + 1))
        }
    }

    fun setCurrent(@IntRange(from = 1) page: Int) {
        Log.i(TAG, "修改当前页: $page")
        this.currentPage = page
        this.viewPager2?.setCurrentItem(page - 1, false)
    }

    override fun handleArguments(bundle: Bundle) {
        this.argument = parseBundle(bundle, TopicListActivity.KEY_ARGUMENT, TopicListArgument::class.java)
    }

    override fun handleMessage(msg: Message): Boolean {
        when (msg.what) {
            HandlerMessageWhat.PAGE_UPDATED.ordinal -> {
                Log.i(TAG, "页码信息更新: ${msg.arg1}/${msg.arg2}")
                this.currentPage = if (msg.arg1 > 0) msg.arg1 else this.currentPage
                this.totalPage = if (msg.arg2 > 0) msg.arg2 else this.totalPage
                findParentHandler().sendMessage(HandlerMessageWhat.PAGE_UPDATED, msg.arg1, msg.arg2)
                return true
            }

            HandlerMessageWhat.CURRENT_FORUM_UPDATE.ordinal -> {
                this.currentForum = msg.obj as? CurrentForum
                val forum = this.currentForum
                if (forum != null) {
                    // 当前板面ID和标题
                    val title = forum.forumName ?: forum.colTitle
                    val id = forum.forumId ?: forum.colTid
                    Log.i(TAG, "当前版面信息更新: $title,id: $id")
                }
            }
        }
        return super.handleMessage(msg)
    }

    /**
     * 刷新操作
     */
    fun refresh() {
        if (currentPageFragment.b?.swipeRefreshLayout?.isRefreshing == false) currentPageFragment.refresh()
    }

}